{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load inventree_extras %}

{% block page_title %}
{% inventree_title %} | {% trans "Index" %}
{% endblock page_title %}

{% block breadcrumb_list %}
{% endblock breadcrumb_list %}

{% block sidebar %}
<!-- Sidebar data is filled dynamically for the index page-->
{% endblock sidebar %}

{% block content %}

<div id='detail-panels'>
</div>

{% endblock content %}

{% block js_ready %}

{{ block.super }}

{% settings_value 'HOMEPAGE_HIDE_INACTIVE' user=request.user as hide_inactive %}
{% settings_value 'HOMEPAGE_PART_STARRED' user=request.user as setting_part_starred %}
{% settings_value 'HOMEPAGE_CATEGORY_STARRED' user=request.user as setting_category_starred %}
{% settings_value 'HOMEPAGE_PART_LATEST' user=request.user as setting_part_latest %}
{% settings_value 'HOMEPAGE_BOM_REQUIRES_VALIDATION' user=request.user as setting_bom_validation %}
{% to_list setting_part_starred setting_part_latest setting_bom_validation as settings_list_part %}

{% if roles.part.view and True in settings_list_part %}
addHeaderTitle('{% trans "Parts" %}');

{% if setting_part_starred %}
addHeaderAction('starred-parts', '{% trans "Subscribed Parts" %}', 'fa-bell');
loadSimplePartTable("#table-starred-parts", "{% url 'api-part-list' %}", {
    name: 'starred-parts',
    params: {
        starred: true,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
});
{% endif %}

{% if setting_category_starred %}
addHeaderAction('starred-categories', '{% trans "Subscribed Categories" %}', 'fa-bell');
loadPartCategoryTable($('#table-starred-categories'), {
    params: {
        starred: true,
    },
    name: 'starred_categories'
});
{% endif %}

{% if setting_part_latest %}
addHeaderAction('latest-parts', '{% trans "Latest Parts" %}', 'fa-newspaper');
loadSimplePartTable("#table-latest-parts", "{% url 'api-part-list' %}", {
    name: 'latest-parts',
    params: {
        ordering: "-creation_date",
        created_after: moment().subtract(1, 'months').format('YYYY-MM-DD'),
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: 'latest_parts',
});
{% endif %}

{% if setting_bom_validation %}
addHeaderAction('bom-validation', '{% trans "BOM Waiting Validation" %}', 'fa-times-circle');
loadSimplePartTable("#table-bom-validation", "{% url 'api-part-list' %}", {
    name: 'parts-invalid-bom',
    params: {
        "bom_valid": false,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: 'bom_invalid_parts',
});
{% endif %}
{% endif %}

{% settings_value 'HOMEPAGE_STOCK_RECENT' user=request.user as setting_stock_recent %}
{% settings_value 'HOMEPAGE_STOCK_LOW' user=request.user as setting_stock_low %}
{% settings_value 'HOMEPAGE_SHOW_STOCK_DEPLETED' user=request.user as setting_stock_depleted %}
{% settings_value 'HOMEPAGE_BUILD_STOCK_NEEDED' user=request.user as setting_stock_needed %}
{% settings_value "STOCK_ENABLE_EXPIRY" as expiry %}
{% if expiry %}
{% settings_value 'HOMEPAGE_STOCK_EXPIRED' user=request.user as setting_stock_expired %}
{% settings_value 'HOMEPAGE_STOCK_STALE' user=request.user as setting_stock_stale %}
{% to_list setting_stock_recent setting_stock_low setting_stock_depleted setting_stock_needed setting_stock_expired setting_stock_stale as settings_list_stock %}
{% else %}
{% to_list setting_stock_recent setting_stock_low setting_stock_depleted setting_stock_needed as settings_list_stock %}
{% endif %}

{% if roles.stock.view %}
{% if setting_stock_recent %}
addHeaderAction('recently-updated-stock', '{% trans "Recently Updated" %}', 'fa-clock');
loadStockTable($('#table-recently-updated-stock'), {
    disableFilters: true,
    params: {
        in_stock: true,
        part_detail: true,
        ordering: "-updated",
        updated_after: moment().subtract(1, 'months').format('YYYY-MM-DD'),
    },
    name: 'recently-updated-stock',
});
{% endif %}

{% if setting_stock_low %}
addHeaderAction('low-stock', '{% trans "Low Stock" %}', 'fa-flag');
loadSimplePartTable("#table-low-stock", "{% url 'api-part-list' %}", {
    name: 'parts-low-stock',
    params: {
        low_stock: true,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: "low_stock_parts",
});
{% endif %}

{% if setting_stock_depleted %}
addHeaderAction('depleted-stock', '{% trans "Depleted Stock" %}', 'fa-times');
loadSimplePartTable("#table-depleted-stock", "{% url 'api-part-list' %}", {
    name: 'parts-depleted-stock',
    params: {
        depleted_stock: true,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: "depleted_stock_parts",
});
{% endif %}

{% if setting_stock_needed %}
addHeaderAction('stock-to-build', '{% trans "Required for Build Orders" %}', 'fa-bullhorn');

loadRequiredForBuildsPartsTable("#table-stock-to-build", {});
{% endif %}

{% if expiry %}

{% if setting_stock_expired %}
addHeaderAction('expired-stock', '{% trans "Expired Stock" %}', 'fa-calendar-times');
loadStockTable($("#table-expired-stock"), {
    disableFilters: true,
    params: {
        expired: true,
        location_detail: true,
        part_detail: true,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: 'expired-stock',
});
{% endif %}

{% if setting_stock_stale %}
addHeaderAction('stale-stock', '{% trans "Stale Stock" %}', 'fa-stopwatch');
loadStockTable($("#table-stale-stock"), {
    disableFilters: true,
    params: {
        stale: true,
        expired: false,
        location_detail: true,
        part_detail: true,
        {% if hide_inactive %}
        active: true,
        {% endif %}
    },
    name: 'stale-stock',
});
{% endif %}

{% endif %}
{% endif %}

{% settings_value 'HOMEPAGE_BUILD_PENDING' user=request.user as setting_build_pending %}
{% settings_value 'HOMEPAGE_BUILD_OVERDUE' user=request.user as setting_build_overdue %}
{% to_list setting_build_pending setting_build_overdue as settings_list_build %}

{% if roles.build.view and True in settings_list_build %}
addHeaderTitle('{% trans "Build Orders" %}');

{% if setting_build_pending %}
addHeaderAction('build-pending', '{% trans "Build Orders In Progress" %}', 'fa-cogs');
loadBuildTable("#table-build-pending", {
    locale: '{{ request.LANGUAGE_CODE }}',
    params: {
        active: true,
    },
    disableFilters: true,
});
{% endif %}

{% if setting_build_overdue %}
addHeaderAction('build-overdue', '{% trans "Overdue Build Orders" %}', 'fa-calendar-times');
loadBuildTable("#table-build-overdue", {
    locale: '{{ request.LANGUAGE_CODE }}',
    params: {
        overdue: true,
    },
    disableFilters: true,
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_PO_OUTSTANDING' user=request.user as setting_po_outstanding %}
{% settings_value 'HOMEPAGE_PO_OVERDUE' user=request.user as setting_po_overdue %}
{% to_list setting_po_outstanding setting_po_overdue as settings_list_po %}

{% if roles.purchase_order.view and True in settings_list_po %}
addHeaderTitle('{% trans "Purchase Orders" %}');

{% if setting_po_outstanding %}
addHeaderAction('po-outstanding', '{% trans "Outstanding Purchase Orders" %}', 'fa-sign-in-alt');
loadPurchaseOrderTable("#table-po-outstanding", {
    url: "{% url 'api-po-list' %}",
    params: {
        supplier_detail: true,
        outstanding: true,
    }
});
{% endif %}

{% if setting_po_overdue %}
addHeaderAction('po-overdue', '{% trans "Overdue Purchase Orders" %}', 'fa-calendar-times');
loadPurchaseOrderTable("#table-po-overdue", {
    url: "{% url 'api-po-list' %}",
    params: {
        supplier_detail: true,
        overdue: true,
    }
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_SO_OUTSTANDING' user=request.user as setting_so_outstanding %}
{% settings_value 'HOMEPAGE_SO_OVERDUE' user=request.user as setting_so_overdue %}
{% settings_value 'HOMEPAGE_SO_SHIPMENTS_PENDING' user=request.user as setting_so_shipments_pending %}
{% to_list setting_so_outstanding setting_so_overdue setting_so_shipments_pending as settings_list_so %}

{% if roles.sales_order.view and True in settings_list_so %}
addHeaderTitle('{% trans "Sales Orders" %}');

{% if setting_so_outstanding %}
addHeaderAction('so-outstanding', '{% trans "Outstanding Sales Orders" %}', 'fa-sign-out-alt');
loadSalesOrderTable("#table-so-outstanding", {
    url: "{% url 'api-so-list' %}",
    params: {
        customer_detail: true,
        outstanding: true,
    },
});
{% endif %}

{% if setting_so_overdue %}
addHeaderAction('so-overdue', '{% trans "Overdue Sales Orders" %}', 'fa-calendar-times');
loadSalesOrderTable("#table-so-overdue", {
    url: "{% url 'api-so-list' %}",
    params: {
        overdue: true,
        customer_detail: true,
    }
});
{% endif %}

{% if setting_so_shipments_pending %}
addHeaderAction('so-shipments', '{% trans "Pending Shipments" %}', 'fa-truck-loading');
loadSalesOrderShipmentTable("#table-so-shipments", {
    url: "{% url 'api-so-shipment-list' %}",
    params: {
        shipped: false,
    },
    show_so_reference: true,
});
{% endif %}

{% endif %}

{% settings_value 'HOMEPAGE_NEWS' user=request.user as setting_news %}
{% if setting_news and user.is_staff %}

addHeaderTitle('{% trans "InvenTree News" %}');

addHeaderAction('news', '{% trans "Current News" %}', 'fa-newspaper');
loadNewsFeedTable("#table-news", {
    url: "{% url 'api-news-list' %}",
});
{% endif %}

enableSidebar(
    'index',
    {
        hide_toggle: true,
    }
);

{% endblock js_ready %}
